<template>
	<view><button type="primary" @tap="toPay('微信支付')">微信支付</button></view>
</template>

<script>
import __localstorage from '@/common/localstorage/localstorage.js';
import weixinpayapi from '@/common/mpweixin/weixinpay.js';
export default {
	data() {
		return {
			code: '',
			myopenid: '',
			orderid: '11111111111111'
		};
	},
	onLoad() {
		this.initPaymentrequest();
	},
	methods: {
		initPaymentrequest() {
			let that = this;
			let channel_from = '';

			let openid = __localstorage.storeOpen.getMyopenid();
			if (openid) return (this.myopenid = openid);
			// 				// #ifdef H5
			// 				channel_from = 'H5';
			//
			// 				if (this.$wxbroswor) {
			// 					this.getjssdkCode((code) => {
			// 						weixinpayapi.getOpenid(code,channel_from,(_openid)=>{
			// 							that.myopenid = _openid
			// 						})
			// 					});
			// 				}
			// 				// #endif
			// 				// #ifdef MP-WEIXIN
			// 				channel_from = 'MP';
			// 				weixinpayapi.checkLogin_MP((code) => {
			// 					weixinpayapi.getOpenid(code,channel_from,(_openid)=>{
			// 						that.myopenid = _openid
			// 					})
			// 				});
			// 				// #endif
		},
		getjssdkCode(callback) {
			let backurl = encodeURIComponent(window.location.href);
			let code = weixinpayapi.getUrlKey('code');
			if (!code) {
				window.location.href =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
					this.$miniProgramID +
					'&redirect_uri=' +
					backurl +
					'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
			} else {
				if (callback) callback(code);
			}
		},
		//1
		setWeixinpayH5() {
			let that = this;

			weixinpayapi.setWeixinpay({
				orderid: that.orderid,
				channel_from: 'H5',
				openid: that.myopenid,
				callback: that.jsSdk
			});
		},
		//2
		jsSdk(params) {
			let _this = this;
			if (typeof window.WeixinJSBridge === 'undefined') {
				if (document.addEventListener) {
					document.addEventListener(
						'WeixinJSBridgeReady',
						function() {
							_this.onBridgeReady(params);
						},
						false
					);
				} else if (document.attachEvent) {
					document.attachEvent('WeixinJSBridgeReady', function() {
						_this.onBridgeReady(params);
					});
					document.attachEvent('onWeixinJSBridgeReady', function() {
						_this.onBridgeReady(params);
					});
				}
			} else {
				_this.onBridgeReady(params);
			}
		},
		//3
		onBridgeReady(params) {
			let _this = this;
			uni.showLoading({
				mask: true
			});
			window.WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				{
					appId: _this.$miniProgramID, // 公众号名称，由商户传入
					timeStamp: params.timeStamp, // 时间戳，自1970年以来的秒数
					nonceStr: params.nonceStr, // 随机串
					package: params.package,
					signType: params.signType, // 微信签名方式：
					paySign: params.paySign // 微信签名
				},
				function(res) {
					uni.hideLoading();
					let msg = '';
					if (res.err_msg == '') {
						msg = '支付完成';
					} else {
						msg = '已取消支付';
					}
					uni.showModal({
						content: msg,
						showCancel:false
					});
				}
			);
		},
		//1
		setWeixinpayMP() {
			let that = this;
			let go = function(tag) {
				uni.showLoading({
					mask: true
				});

				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: tag.timeStamp,
					nonceStr: tag.nonceStr,
					package: tag.package,
					signType: tag.signType,
					paySign: tag.paySign,
					success(payres) {
						let msg = '';
						uni.showModal({
							content: '支付完成',
							showCancel:false
						});
					},
					fail(error) {
						console.log(error);
						uni.showModal({
							content: '已取消支付',
							showCancel:false
						});
					},
					complete() {
						uni.hideLoading();
					}
				});
			};

			weixinpayapi.setWeixinpay({
				orderid: that.orderid,
				openid: that.myopenid,
				channel_from: 'MP',
				callback: go
			});
		},
		toPay(payment) {
			if (payment !== '微信支付') {
			} else {
				// #ifdef MP-WEIXIN
				this.setWeixinpayMP();
				// #endif
				// #ifdef H5
				this.setWeixinpayH5();
				// #endif
			}
		}
	}
};
</script>

<style></style>
